package com.fs.console.base.command;

import com.fs.common.pojo.Health;
import com.fs.common.pojo.WechatPayConfig;
import com.fs.common.utils.ConcurrentHashMapCacheUtils;
import com.fs.common.yzMapper.HealthMapper;
import com.fs.common.yzMapper.WechatPayConfigMapper;
import com.fs.console.base.param.SysConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author TiddlerCJ
 * @desc 加载模块数据
 */
@Slf4j
@Component
@Order(value=1)
public class LoadModleDataCommand implements CommandLineRunner {

	@Autowired
	private HealthMapper healthMapper;
	@Autowired
	private WechatPayConfigMapper wechatPayConfigMapper;

	@Override
	public void run(String... args) throws Exception {
		loadHealthToCache();
		cacheWechatPayConfig();
		log.info(" Load ALL Data success! ");
	}

	/**
	 * 将症状相关信息加入缓存中
	 */
	private void loadHealthToCache(){
		List<Health> diseaseList = healthMapper.selectList(null);
		for (Health h : diseaseList) {
			SysConstant.HEALTH_NAME.put(h.getName(),h);
		}
	}

	private void cacheWechatPayConfig(){
        WechatPayConfig wechatPayConfig = wechatPayConfigMapper.selectById(1);
        if (wechatPayConfig != null) {
            ConcurrentHashMapCacheUtils.setCache(WechatPayConfig.CACHE_KEY,wechatPayConfig);
        }
    }
}
